<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
// use app\admin\model\Employ as EmployModel;//管理员模型
use app\admin\controller\Permissions;
class ContactUs extends Permissions
{
    /**
     * 招聘信息列表
     */
    public function index()
    {

        $name=trim($this->request->Post('name'));
        $state=$this->request->Post('state');
        if($name !='') $where['name']= ['like','%' . $name . '%'];
        if($state !='') $where['state']= ['=',$state];


        $contact_us = empty($where)?Db::name('contact_us')->order("id","desc")->paginate(10): Db::name('contact_us')->where($where)->order("id","desc")->paginate(10,true,['query'=>request()->Post()]);
        $this->assign('contact_us',$contact_us);
        // var_dump($news);
        return $this->fetch();
    }

    /**
     * 添加招聘信息
     */
    public function add(){
        // var_dump($this->request->has('id'));
        //判断是不是操作数据库  
        if($this->request->isPost()){
            $post=$this->request->post();
            // 验证  唯一规则： 表名，字段名，排除主键值，主键名

                $validate = new \think\Validate([
                    ['jingdu', 'require|number', '经度格式不正确，请填写数字!'],
                    ['weidu', 'require|number', '纬度格式不正确，请填写数字!'],
                ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
             $data=[
                    'name'=>$post['name'],
                    'jingdu'=>$post['jingdu'],
                    'weidu'=>$post['weidu'],
                    'content'=>$post['content'],
                    'phone'=>$post['phone'],
                    'email'=>$post['email'],
                    'zhidao'=>$post['zhidao'],
                ];
            

            $id=$this->request->Post("id");
            // return $post;
            if($id==''){       
                $ress=Db::name("contact_us")->insert($data);
                if($ress==false) return $this->error('修改失败');   
            }else{
                $res = Db::name("contact_us")->where("id",$id)->update($data);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success("信息保存成功",'admin/contact_us/index');
        }elseif($this->request->has('id')){
           //获取对应数据id
        $id=$this->request->param("id");
        $contact_us=Db::name("contact_us")->where("id",$id)->find();
        // var_dump($employ);
        $this->assign("contact_us",$contact_us);
        return $this->fetch("contact_us/add");
        }else{
             return $this->fetch();
        }
    }

    /**
     * 修改页面
     */
    public function edit(){
        //获取对应数据id
        $id=$this->request->param("id");
        $contact_us=Db::name("contact_us")->where("id",$id)->find();
        // var_dump($employ);
        $this->assign("contact_us",$contact_us);
        return $this->fetch("contact_us/add");
    } 


     /**
     * 新闻删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('contact_us')->where('id',$id)->delete()) {
                return 1;
            } else {
                return 0;
            }
        }
    }
  
}
